<template>
  <div>
    <el-form :inline="true"
             class="coverage-style-searchForm">
      <el-form-item>
        <div class="demo-input-suffix">
          <el-input placeholder="请输入菜单名称"
                    clearable
                    v-model="menuName"
                    prefix-icon="el-icon-search"
                    class="key-word-search">
          </el-input>
        </div>
      </el-form-item>
      <el-form-item>
        <button @click="getSelectAll"
                type="button"
                class="el-button el-button--primary el-button--medium h30Btn ">查询</button>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading"
              stripe
              border
              :header-cell-style="{background:'#F2F3F8'}"
              tooltip-effect="dark"
              :data="tableData"
              style="width: 100%">
      <el-table-column prop="menuName"
                       label="菜单名称"
                       align="center"
                       min-width="14%">
        <template slot-scope="scope">
          <span style="color: #2393f1">{{scope.row.menuName}}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态"
                       align="center"
                       min-width="10%">
        <template slot-scope="scope">
          <span v-if="scope.row.status === 1">启用</span>
          <span v-else>停用</span>
        </template>
      </el-table-column>
      <el-table-column label="属性"
                       align="center"
                       min-width="10%">
        <template slot-scope="scope">
          <span v-if="scope.row.tagId === 1">系统内置</span>
          <span v-else>自定义</span>
        </template>
      </el-table-column>
      <el-table-column label="工单流程"
                       align="center"
                       min-width="10%">
        <template slot-scope="scope">{{ scope.row.workOrderTypeName || '无'}}</template>
      </el-table-column>
      <el-table-column label="可见范围"
                       align="center"
                       min-width="10%">

        <template slot-scope="scope">
          <el-popover trigger="hover"
                      placement="top">
            <p>{{ scope.row.menuVisibleNames }}</p>
            <div slot="reference"
                 class="name-wrapper">
              <p>{{ stringSlice(scope.row.menuVisibleNames) }}</p>
            </div>
          </el-popover>
        </template>
        <!-- <template slot-scope="scope">
          <p v-if="scope.row.visibleRange.nameList === ''">{{ scope.row.visibleRange.type }}</p>
          <el-popover trigger="hover"
                      placement="top"
                      v-else>
            <p>{{ scope.row.visibleRange.nameList }}</p>
            <div slot="reference"
                 class="name-wrapper">
              <p>{{ stringSlice(scope.row.visibleRange.nameList) }}</p>
            </div>
          </el-popover>
        </template> -->
      </el-table-column>
      <el-table-column label="最近修改时间"
                       align="center"
                       min-width="14%">
        <template slot-scope="scope">{{ timestampToTime(scope.row.lastUpdateTime) }}</template>
      </el-table-column>
      <el-table-column label="最近修改人"
                       align="center"
                       min-width="10%">
        <template slot-scope="scope">{{ scope.row.lastUserName }}</template>
      </el-table-column>
      <el-table-column label="操作"
                       show-overflow-tooltip
                       align="center"
                       min-width="22%">
        <template slot-scope="scope">
          <el-button type="text"
                     size="mini"
                     v-if="scope.row.tagId === 1"
                     disabled
                     class="run-and-stop"
                     @click="handleOff(scope.$index, scope.row)">
            <!-- <i class="el-icon-circle-check"></i> -->
            <img v-if="scope.row.status !== 1"
                 src="../../../assets/images/start.png"
                 alt />
            <img v-else
                 src="../../../assets/images/stop.png"
                 alt />
            <span v-if="scope.row.status !== 1">启用</span>
            <span v-else>停用</span>
          </el-button>
          <el-button type="text"
                     size="mini"
                     v-else-if="scope.row.tagId !== 1"
                     class="run-and-stop"
                     @click="handleOff(scope.$index, scope.row)">
            <!-- <i class="el-icon-circle-check"></i> -->
            <img v-if="scope.row.status !== 1"
                 src="../../../assets/images/start.png"
                 alt />
            <img v-else
                 src="../../../assets/images/stop.png"
                 alt />
            <span v-if="scope.row.status !== 1">启用</span>
            <span v-else>停用</span>
          </el-button>
          <!-- <el-button
            icon="el-icon-edit"
            type="text"
            size="mini"
            disabled
            v-if="scope.row.tagId === 1"
            @click="handleEdit(scope.$index, scope.row)"
          >编辑</el-button> -->
          <el-button icon="el-icon-edit"
                     type="text"
                     size="mini"
                     @click="handleEdit(scope.$index, scope.row)">编辑
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-row>
      <el-pagination background
                     class="page-foot-spacing el-pagination-style"
                     v-show="totalDataLength > 0"
                     @size-change="handleSizeChange"
                     @current-change="handleCurrentChange"
                     :current-page="page"
                     :page-sizes="[10, 20, 50, 100]"
                     :page-size="pageSize"
                     layout="total, sizes, prev, pager, next, jumper"
                     :total="totalDataLength"></el-pagination>
    </el-row>
  </div>
</template>
<script>
import { updateMenuUseStatus, selectByName } from '@/api/typeMenu'
import common from '@/common/common.js'

export default {
  data () {
    return {
      loading: true,
      tableData: [],
      totalDataLength: 0,
      page: 1,
      pageSize: 10,
      menuName: ''
    }
  },
  mounted () {
    this.getSelectAll()
  },
  methods: {
    stringSlice (str) {
      if (!str) {
        return
      }
      if (str.length > 3) {
        str = str.substring(0, 3) + '...'
        return str
      } else {
        return str
      }
    },
    getSelectAll () {
      this.page = 1
      this.getSelectAllData()
    },
    // 封装
    getSelectAllData () {
      let params = {
        name: this.menuName,
        currentPage: this.page,
        pageSize: this.pageSize
      }
      selectByName(params).then(res => {
        let data = res.data
        if (data.retCode === 200) {
          this.totalDataLength = data.count
          this.tableData = data.data
          this.loading = false
        }
      })
    },
    handleSizeChange (pageSize) {
      this.pageSize = pageSize
      this.page = 1
      // this.getSelectAll()
      this.getSelectAllData()
    },
    handleCurrentChange (page) {
      this.page = page
      // this.getSelectAll()
      this.getSelectAllData()
    },
    handleOff (index, row) {
      let status = row.status
      if (status === 0) {
        status = 1
      } else {
        status = 0
      }
      let params = {
        id: row.id,
        userId: this.$store.state.userId,
        status: status
      }
      updateMenuUseStatus(params).then(res => {
        let data = res.data
        if (data.retCode === 200) {
          this.$message({
            type: 'success',
            message: data.msg
          })
          // this.$store.commit('workOrderTextFun', params.status)
          // this.getSelectAll()
          this.getSelectAllData()
        } else {
          this.$message({
            type: 'error',
            message: data.msg
          })
        }
      })
    },
    handleSelectionChange (val) {
    },
    handleEdit (index, row) {
      this.$store.commit('typeMenuIdFun', row.id)
      this.$router.push({
        path: '/orderEditMenu'
      })
    },
    // 时间戳的转换
    timestampToTime (timestamp) {
      return common.timestampToTime(timestamp)
    }
  }
}
</script>
<style scoped>
.icon {
  display: inline-block;
  width: 16px;
  height: 16px;
  /* background-image: url('./begin.png'); */
  background-position: center center;
  background-size: 16px 16px;
  background-repeat: no-repeat;
}

>>> .has-gutter > th {
  color: #606266;
}

.key-word-search {
  width: 300px;
}

.el-button--text {
  line-height: 16px;
}

.el-pagination {
  text-align: right;
}

>>> .el-input__icon {
  font-size: 13px;
  font-weight: 800;
}

>>> .el-input--prefix .el-input__inner {
  padding-left: 35px;
}
.el-pagination-style {
  margin: 15px 0;
}
</style>
